package com.guilin.controller;

import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.common.core.page.TableDataInfo;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.domain.News;
import com.guilin.system.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻发布Controller
 *
 * @author xiaoqi
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/admin-management/news")
public class NewsController extends BaseController
{
    @Autowired
    private NewsService newsService;

    /**
     * 查询新闻发布列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出新闻发布列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "新闻发布数据");
    }

    /**
     * 获取新闻发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(newsService.selectNewsById(id));
    }

    /**
     * 新增新闻发布
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:add')")
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改新闻发布
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除新闻发布
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(newsService.deleteNewsByIds(ids));
    }

    /**
     * 发布新闻--修改状态为发布状态
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:edit')")
    @PutMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id) {
        newsService.publishNews(id);
        return success("新闻发布成功");
    }

    /**
     * 撤销新闻--修改发布状态为撤销新闻
     */
    @PreAuthorize("@ss.hasPermi('admin-management:news:edit')")
    @PutMapping("/withdraw/{id}")
    public AjaxResult withdraw(@PathVariable Long id) {
        newsService.withdrawNews(id);
        return success("新闻撤回成功");
    }

    /**
     * 记录新闻浏览量
     */
    @GetMapping("/view/{id}")
    public AjaxResult viewNews(@PathVariable Long id) {
        newsService.updateViewCount(id);
        return success("浏览量统计成功");
    }
}
